use std::env;

use common::utils::current_datetime;
use gen_table::model::{DefaultValueFunc, GenColumn, GenTable};
use sea_orm::{ConnectionTrait, Database, DbConn};

async fn get_db() -> DbConn {
    dotenv::dotenv().ok();
    let databaseurl = env::var("DATABASEURL").unwrap();
    Database::connect(databaseurl).await.unwrap()
}

#[tokio::main]
#[test]
async fn test_init_db() {
    let db = get_db().await;
    let id = GenColumn::new("id", gen_table::model::ColumnType::BigUnsigned)
        .set_pk(true)
        .set_sort(0)
        .set_increment(true);
    let t_id = GenColumn::new("t_id", gen_table::model::ColumnType::BigUnsigned).set_sort(1);
    let name = GenColumn::new("name", gen_table::model::ColumnType::String)
        .column_type_len(Some(64))
        .set_sort(2);
    let column_name = GenColumn::new("column_name", gen_table::model::ColumnType::String)
        .set_required(false)
        .set_sort(3)
        .column_type_len(Some(64));
    let schema = GenColumn::new("schema", gen_table::model::ColumnType::String)
        .set_required(false)
        .set_sort(4)
        .column_type_len(Some(64));
    let comment = GenColumn::new("comment", gen_table::model::ColumnType::String)
        .set_required(false)
        .set_sort(5)
        .column_type_len(Some(64));
    let create_at = GenColumn::new("create_at", gen_table::model::ColumnType::DateTime)
        .set_required(false)
        .set_sort(6)
        .set_default(Some(DefaultValueFunc::DateTime))
        .column_type_len(Some(64));
    let update_at = GenColumn::new("update_at", gen_table::model::ColumnType::DateTime)
        .set_required(false)
        .set_sort(7)
        .set_default(Some(DefaultValueFunc::DateTime))
        .set_update(Some(DefaultValueFunc::DateTime))
        .column_type_len(Some(64));

    let gen_table_column_db = GenTable::new(
        "gen_table_column",
        vec![
            id,
            t_id,
            name,
            column_name,
            schema,
            comment,
            create_at.clone(),
            update_at.clone(),
        ],
    );

    let id = GenColumn::new("id", gen_table::model::ColumnType::BigUnsigned)
        .set_pk(true)
        .set_sort(0)
        .set_increment(true);

    let name = GenColumn::new("name", gen_table::model::ColumnType::String)
        .column_type_len(Some(64))
        .set_required(true)
        .set_sort(1);
    let table_name = GenColumn::new("table_name", gen_table::model::ColumnType::String)
        .column_type_len(Some(64))
        .set_required(true)
        .set_sort(2);
    let schema = GenColumn::new("schema", gen_table::model::ColumnType::String)
        .column_type_len(Some(64))
        .set_required(false)
        .set_sort(3);
    let comment = GenColumn::new("comment", gen_table::model::ColumnType::String)
        .column_type_len(Some(64))
        .set_required(false)
        .set_sort(4);

    let gen_table_db = GenTable::new(
        "gen_table",
        vec![id, name, table_name, schema, comment, create_at, update_at],
    );
    gen_table::Mutation::create_table(&db, &gen_table_db).await;
    gen_table::Mutation::create_table(&db, &gen_table_column_db).await;
}

#[tokio::main]
#[test]
async fn test() {
    let db = get_db().await;
}
